/*
 * @(#)GetDocumentsAndAssociationsRunner.java
 * Date 2013-09-06
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.query.es;

import us.hornerscorners.lamppost.exception.ServiceActionException;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;

import us.hornerscorners.lamppost.ihecommon.service.ElasticSearchSource;

import us.hornerscorners.lamppost.model.IFilter;

import us.hornerscorners.lamppost.model.document.Association;

import us.hornerscorners.lamppost.registry.model.QueryRequest;
import us.hornerscorners.lamppost.registry.model.QueryResponse;
import us.hornerscorners.lamppost.registry.model.RegistryObjects;

import us.hornerscorners.lamppost.registry.query.AssociationSourceOrTargetURNFilter;

import us.hornerscorners.lamppost.registry.transformer.model.CodedElementTransformer;
import us.hornerscorners.lamppost.registry.transformer.model.PatientIdentifierTransformer;

import us.hornerscorners.lamppost.registry.utils.RegistryObjectsUtils;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-06
 * @author         Jim Horner
 */
public class GetDocumentsAndAssociationsRunner extends GetDocumentsRunner {

    /**
     * Constructs ...
     *
     *
     * @param config
     * @param elasticSearchSource
     * @param codedElementTransformer
     * @param patientIdentifierTransformer
     */
    public GetDocumentsAndAssociationsRunner(IHEConfig config,
            ElasticSearchSource elasticSearchSource,
            CodedElementTransformer codedElementTransformer,
            PatientIdentifierTransformer patientIdentifierTransformer) {

        super(config, elasticSearchSource, codedElementTransformer,
              patientIdentifierTransformer);

    }

    /**
     * Method description
     *
     *
     * @param request
     *
     * @return
     *
     * @throws ServiceActionException
     */
    @Override
    public QueryResponse run(QueryRequest request) throws ServiceActionException {

        QueryResponse result = super.run(request);

        RegistryObjects rol = result.getRegistryObjects();
        String[] docEntryURNs = rol.getDocumentEntryURNs();

        IFilter<Association> filter =
            new AssociationSourceOrTargetURNFilter(docEntryURNs);

        rol.addAllAssociations(RegistryObjectsUtils.pullAllAssociations(rol, filter));

        return result;
    }
}
